﻿<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/master/admin.master" CodeFile="manufacturer-list.aspx.cs" Inherits="admin_manufacturer_list" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<%@ Register Namespace="ECommerce.Library.Web" TagPrefix="ucc" %>
<asp:Content ID="Content1" ContentPlaceHolderID="AdminHeaderContent" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cphMiddleContent" runat="Server">
    <script type="text/javascript">
        var IsActive_Renderer = function (value) {
            if (value == true)
                return "Yes";
            else
                return "No";
        }

        var Partner_Render = function (value, p, record, rowIndex) {
            var supplierUrl = "<%= SiteGlobals.BaseUrl %>" + "partner/" + record.data.BrandUrl;
            return '<a href="' + supplierUrl + '" class="leftlink" target="_blank">' + supplierUrl + '</a>';
        };

        var Name_Render = function (value, p, record, rowIndex) {
            var supplierUrl = "<%= AutoGear.BLL.CustomSiteMap.ManufacturerEdit %>" + "?id=" + record.data.ProductBrandId;
            return '<a href="' + supplierUrl + '" class="leftlink">' + record.data.BrandName + '</a>';
        };

        var supplierGrid;

        var deleteSupplier = function () {
            if (supplierGrid != null) {
                supplierGrid.getRowEditor().stopEditing();

                var s = supplierGrid.getSelectionModel().getSelections();
                if (s.length > 0) {

                    if (confirm("Are you sure you want to delete these suppliers?")) {

                        var ids = "";

                        for (var i = 0, r; r = s[i]; i++) {
                            ids = ids + " " + r.data.ProductBrandId;
                        }

                        // Call DirectMethod
                        Polana.WebService.DeleteSupplier(ids);
                    }
                }
            }
        }

        var addSupplier = function () {
            window.location = "<%= SiteGlobals.BaseUrl %>" + "admin/supplier-add.aspx";
        }

        var CommandHandler = function (command, rowIndex, grid) {
            //showObj(grid);
            grid.getRowEditor().stopEditing();
            grid.getView().refresh();
            grid.getSelectionModel().selectRow(rowIndex);
            grid.getRowEditor().startEditing(rowIndex);
        }

        var afterEdit = function (e) {

            e.grid.stopEditing();

            // Call DirectMethod
            Polana.WebService.UpdateSupplier(e.record.data.ProductBrandId, e.record.data.BrandName, e.record.data.BrandDescription, e.record.data.IsActive);
        };


        var onDocumentReady = function (grid) {
            supplierGrid = grid;
        }
      
    </script>
   
    <ext:ResourceManager ID="ScriptManager1" runat="server" Theme="Gray">
        <Listeners>
            <DocumentReady Fn="onDocumentReady(#{GridPanel1})" />
        </Listeners>
    </ext:ResourceManager>
    <ext:Store ID="Store1" runat="server" RemoteSort="true" OnRefreshData="Store1_RefreshData">
        <AutoLoadParams>
            <ext:Parameter Name="sort" Value="BrandName" Mode="Value" />
            <ext:Parameter Name="start" Value="0" Mode="Raw" />
            <ext:Parameter Name="limit" Value="20" Mode="Raw" />
        </AutoLoadParams>
        <Proxy>
            <ext:PageProxy />
        </Proxy>
        <Reader>
            <ext:JsonReader IDProperty="FormsId">
                <Fields>
                    <ext:RecordField Name="ProductBrandId" />
                    <ext:RecordField Name="BrandName" />
                    <ext:RecordField Name="BrandDescription" />
                    <ext:RecordField Name="BrandUrl" />
                    <ext:RecordField Name="IsActive" Type="Boolean" />
                </Fields>
            </ext:JsonReader>
        </Reader>
    </ext:Store>
    <div class="admin-function-top inline">
        <ucc:ResponseMessage ID="uccResponseMessage" runat="server"></ucc:ResponseMessage>
        <ext:GridPanel ID="GridPanel1" runat="server" EnableViewState="true" AutoHeight="true"
            StoreID="Store1">
            <ColumnModel Height="80">
                <Columns>
                    <ext:Column Header="Name" Width="200" DataIndex="BrandName">
                        <Renderer Fn="Name_Render" />
                        <Editor>
                            <ext:TextField ID="txtName" runat="server" />
                        </Editor>
                    </ext:Column>
                     
                    <ext:Column Header="BrandUrl" Width="400" DataIndex="BrandUrl" Sortable="false">
                      <Renderer Fn="Partner_Render" />
                    </ext:Column>
                    <ext:Column Header="Active" DataIndex="IsActive" Width="50" Sortable="false">
                        <Renderer Fn="IsActive_Renderer" />
                        <Editor>
                            <ext:Checkbox ID="chkIsActive" runat="server" />
                        </Editor>
                    </ext:Column>
                    <ext:CommandColumn Width="50">
                        <Commands>
                            <ext:GridCommand Icon="NoteEdit" CommandName="Edit" Text="Edit" />
                        </Commands>
                    </ext:CommandColumn>
                </Columns>
            </ColumnModel>
            <TopBar>
                <ext:Toolbar ID="ActionToolBar" runat="server">
                    <Items>
                        <ext:Button ID="btnToolbarAddCategory" runat="server" Text="Add Partner" Icon="Add">
                            <Listeners>
                                <Click Handler="addSupplier();" />
                            </Listeners>
                        </ext:Button>
                        <ext:ToolbarFill ID="ToolbarFill1" runat="server" />
                        <ext:Button ID="btnToolbarDelete" runat="server" Text="Delete selected" Icon="Delete">
                            <Listeners>
                                <Click Handler="deleteSupplier();" />
                            </Listeners>
                        </ext:Button>
                    </Items>
                </ext:Toolbar>
            </TopBar>
            <BottomBar>
                <ext:PagingToolbar ID="PagingToolbar1" runat="server" PageSize="50" DisplayInfo="true"
                    DisplayMsg="Displaying suppliers {0} - {1} of {2}" EmptyMsg="No supplier to display"
                    AutoWidth="true" />
            </BottomBar>
             <LoadMask ShowMask="true" />
             <SelectionModel>
                <ext:CheckboxSelectionModel ID="CheckboxSelectionModel1" runat="server" ColumnPosition="4"
                    AllowDeselect="true" CheckOnly="true" />
            </SelectionModel>
                        <Plugins>
                <ext:RowEditor ID="rowedi" runat="server" SaveText="Update" ClicksToEdit="2" ErrorSummary="false"
                    ShowTooltip="={Ext.emptyFn}">
                    <Listeners>
                        <AfterEdit Fn="afterEdit" />
                    </Listeners>
                </ext:RowEditor>
            </Plugins>
            <Listeners>
                <Command Handler="CommandHandler(command, rowIndex, this)" />
            </Listeners>
            <View>
                <ext:GridView ID="GridView1" runat="server" ForceFit="true" />
            </View>
        </ext:GridPanel>
    </div>
    <script type="text/javascript">
        Ext.override(Ext.grid.GridView, {
            getEditorParent: function () {
                return document.body;
            }
        });
    </script>
</asp:Content>
